import { ConfigEnv, defineConfig, loadEnv, UserConfig } from 'vite'
import react from '@vitejs/plugin-react'
import { proxyApiTarget } from './src/config'
import { resolve } from 'path'

const CWD = process.cwd()

export default ({ command, mode }: ConfigEnv): UserConfig => {
  // 环境变量
  const { VITE_BASE_URL } = loadEnv(mode, CWD)

  return {
    plugins: [react()],
    base: VITE_BASE_URL,
    server: {
      port: 9527,
      proxy: {
        '/test/api': {
          target: proxyApiTarget,
          changeOrigin: true,
          rewrite: path => path.replace(/^\/api/, '')
        }
      }
    },
    resolve: {
      alias: {
        '@': resolve(__dirname, './src')
      }
    },
    css: {
      modules: {
        generateScopedName: '[name]__[local]__[hash:base64:5]',
        hashPrefix: 'prefix'
      },
      preprocessorOptions: {
        less: {
          javascriptEnabled: true
        }
      }
    }
  }
}
